package org.dromara.lsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.dromara.common.core.service.OssService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.lsc.domain.ModelLsc;
import org.dromara.lsc.domain.TrainStragyLsc;
import org.dromara.lsc.domain.bo.TrainStragyLscBo;
import org.dromara.lsc.domain.vo.DatasetLscVo;
import org.dromara.lsc.domain.vo.TrainStragyLscVo;
import org.dromara.lsc.mapper.DatasetLscMapper;
import org.dromara.lsc.mapper.TrainStragyLscMapper;
import org.dromara.lsc.service.ITrainStragyLscService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Lion Li
 * @date 2023-12-13
 */
@RequiredArgsConstructor
@Service
public class TrainStragyLscServiceImpl implements ITrainStragyLscService {

    private final TrainStragyLscMapper baseMapper;

    private final DatasetLscMapper lscMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public TrainStragyLscVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<TrainStragyLscVo> queryPageList(TrainStragyLscBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TrainStragyLsc> lqw = buildQueryWrapper(bo);
        Page<TrainStragyLscVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<TrainStragyLscVo> queryList(TrainStragyLscBo bo) {
        LambdaQueryWrapper<TrainStragyLsc> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TrainStragyLsc> buildQueryWrapper(TrainStragyLscBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TrainStragyLsc> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTrainId() != null, TrainStragyLsc::getTrainId, bo.getTrainId());
        lqw.eq(bo.getValidId() != null, TrainStragyLsc::getValidId, bo.getValidId());
        lqw.eq(bo.getTrainStatus() != null, TrainStragyLsc::getTrainStatus, bo.getTrainStatus());
        lqw.eq(bo.getModelId() != null, TrainStragyLsc::getModelId, bo.getModelId());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(TrainStragyLscBo bo) {
        TrainStragyLsc add = MapstructUtils.convert(bo, TrainStragyLsc.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(TrainStragyLscBo bo) {
        TrainStragyLsc update = MapstructUtils.convert(bo, TrainStragyLsc.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TrainStragyLsc entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public String getOssId(Long id) {
        TrainStragyLscVo trainStragyLscVo = baseMapper.selectVoById(id);
        return lscMapper.selectVoById(trainStragyLscVo.getTrainId()).getDataFile();
    }

    @Override
    public int updateStatus(TrainStragyLsc bo) {
        return baseMapper.update(
            new LambdaUpdateWrapper<TrainStragyLsc>()
                .eq(TrainStragyLsc::getId, bo.getId())
                .set(TrainStragyLsc::getTrainStatus, bo.getTrainStatus())
        );
    }
}
